﻿/////////////////////////////////////////////////////////////////////
//
//	Map_FVG C# Class Library.
//
//	LineeDialog
//	
//
//	autore: Gustavo Mainardi 
//	versione: 1.0
//	data: 01/12/2015
//
//
//	Cronologia delle versioni:
//
//	Versione 1.0 01/12/2015
//		versione originale
//
//////////////////////////////////////////////////////////////////////////
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Globalization;
using System.Data.SQLite;


namespace Map_Fvg
{
    public partial class LineeDialog : Form
    {

        public LineeDialog()
        {
            InitializeComponent();

            if (ViewQ.readViewQ == 0) return;
            dataGridView1.ColumnCount = 5;
            this.dataGridView1.Columns[0].HeaderText = "codice";
            this.dataGridView1.Columns[1].HeaderText = "denominazione";
            this.dataGridView1.Columns[2].HeaderText = "tipo penna";
            this.dataGridView1.Columns[3].HeaderText = "tratto penna";
            this.dataGridView1.Columns[4].HeaderText = "colore penna";
 //           this.dataGridView1.Columns[5].HeaderText = "presente in archivio";

            this.dataGridView1.Columns[0].Width = 70;
            this.dataGridView1.Columns[1].Width = 310;
            this.dataGridView1.Columns[2].Width = 50;
            this.dataGridView1.Columns[3].Width = 210;
            this.dataGridView1.Columns[4].Width = 100;
   //         this.dataGridView1.Columns[5].Width = 80;

            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.AllowUserToDeleteRows = false;

            int iSel = 0;
            for (int i = 0; i < ViewQ.nSimb; ++i)
            {
                if (ViewQ.vectS[i].selected == 0) continue;
                string[] row = new string[] {
                     ViewQ.vectS[i].codice.ToString(),ViewQ.vectS[i].denom.ToString(),ViewQ.vectS[i].penType.ToString(),
                    ViewQ.vectS[i].tratto};
                //    ViewQ.vectS[i].penWidth.ToString()}; ,ViewQ.vectS[i].icolor.ToString()};
                //,ViewQ.vectS[i].visib.ToString()};
                dataGridView1.Rows.Add(row);
                dataGridView1[4, iSel].Style.BackColor = ViewQ.vectS[i].icolor;
                ++iSel;
            }

            textBox1.Text = iSel.ToString();

            button1.DialogResult = DialogResult.Cancel;
            button2.DialogResult = DialogResult.OK;

            DataGridViewButtonColumn buttonColumn = new DataGridViewButtonColumn();
            buttonColumn.HeaderText = "";
            buttonColumn.Name = "Color";
            buttonColumn.Text = "V";
            buttonColumn.UseColumnTextForButtonValue = true;
            buttonColumn.Width = 20;

            dataGridView1.Columns.Insert(5, buttonColumn);
            // Add a CellClick handler to handle clicks in the button column.
            dataGridView1.CellClick +=
                 new DataGridViewCellEventHandler(this.dataGridView1_CellClick);

            //         this.SizeChanged += new System.EventHandler(this.ViewQ_SizeChanged);
            //       this.Activated += new System.EventHandler(this.ViewQ_Activated);
            //     this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.ViewQ_FormClosing);

            DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn();
            {
                //        column.HeaderText = ColumnName.OutOfOffice.ToString();
                //      column.Name = ColumnName.OutOfOffice.ToString();
                column.AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;
                //      DataGridViewAutoSizeColumnMode.DisplayedCells;
                column.FlatStyle = FlatStyle.Standard;
                column.ThreeState = false;
                column.CellTemplate = new DataGridViewCheckBoxCell();
                column.CellTemplate.Style.BackColor = Color.White;
                column.HeaderText = "visibile";
            }

            int iCol = 6;
            dataGridView1.Columns.Insert(iCol, column);

            iSel = 0;
            for (int i = 0; i < ViewQ.nSimb; ++i)
            {
                if (ViewQ.vectS[i].selected == 0) continue;


                if (ViewQ.vectS[i].visib == 0) dataGridView1[iCol, iSel].Value = false;
                else
                {
                    dataGridView1[iCol, iSel].Value = true;
                }
                ++iSel;
            }

            DataGridViewCheckBoxColumn column1 = new DataGridViewCheckBoxColumn();
            {
                //        column.HeaderText = ColumnName.OutOfOffice.ToString();
                //      column.Name = ColumnName.OutOfOffice.ToString();
                column1.AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;
                //      DataGridViewAutoSizeColumnMode.DisplayedCells;
                column1.FlatStyle = FlatStyle.Standard;
                column1.ThreeState = false;
                column1.CellTemplate = new DataGridViewCheckBoxCell();
                column1.CellTemplate.Style.BackColor = Color.White;
                column1.HeaderText = "presente in archvio";
            }

            iCol = 7;
            dataGridView1.Columns.Insert(iCol, column1);

            using (SQLiteConnection conn = SetConnection())
            {
                conn.Open();
                for (int i = 0; i < ViewQ.nSimb; ++i)
                {
                    if (ViewQ.vectS[i].selected == 0) continue;

                    using (SQLiteCommand cmd = new SQLiteCommand(conn))
                    {
                        cmd.CommandText = "select * from linea where codice_stclasse = '" + ViewQ.vectS[i].codice.ToString() + "'";

                        SQLiteDataReader reader = cmd.ExecuteReader();
                        if (reader.HasRows) dataGridView1[iCol, i].Value = true;
                    }
                }
                conn.Close();
            }
            
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex != 5) return;
            ColorDialog colorDialog1 = new ColorDialog();
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                dataGridView1[4, e.RowIndex].Style.BackColor = colorDialog1.Color;
            }

        }

        private void button2_MouseClick(object sender, MouseEventArgs e)
        {
            int count = dataGridView1.Rows.Count;
            NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;

            for (int i = 0; i < count; ++i)
            {
                string row = dataGridView1[0, i].Value.ToString();
                for (int j = 0; j < ViewQ.nSimb; ++j)
                {
                    int comp = string.Compare(ViewQ.vectS[i].elemento, 0, ViewQ.selectedElement, 0, 6);
                    comp = 0;
                    if (comp == 0)
                    {
                        if (ViewQ.vectS[j].codice.Contains(row))
                        {
                            ViewQ.vectS[j].penType = int.Parse(dataGridView1[2, i].Value.ToString());
                            ViewQ.vectS[j].tratto = string.Copy(dataGridView1[3, i].Value.ToString());
                            //                ViewQ.vectS[j].penWidth = float.Parse(dataGridView1[3, i].Value.ToString(), nfi);
                            ViewQ.vectS[j].icolor = dataGridView1[4, i].Style.BackColor;

                            if ((bool)dataGridView1[6, i].Value) ViewQ.vectS[j].visib = 1;
                            else ViewQ.vectS[j].visib = 0;
                            break;
                        }
                    }
                }
            }
            this.Close();
        }

        private void button1_MouseClick(object sender, MouseEventArgs e)
        {

            this.Close();
        }

        private DataGridViewCell clickedCell;

        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            // If the user right-clicks a cell, store it for use by the shortcut menu.
            if (e.Button == MouseButtons.Right)
            {
                DataGridView.HitTestInfo hit = dataGridView1.HitTest(e.X, e.Y);
                if (hit.Type == DataGridViewHitTestType.Cell)
                {
                    clickedCell =
                        dataGridView1.Rows[hit.RowIndex].Cells[hit.ColumnIndex];
                }
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            int count = this.dataGridView1.RowCount;
            bool bChecked = (bool)checkBox1.Checked;
            if (bChecked)
            {
                for (int j = 0; j < count; ++j)
                {
                    dataGridView1[6, j].Value = false;
                }
            }
            else
            {
                for (int j = 0; j < count; ++j)
                {
                    dataGridView1[6, j].Value = true;
                }
            }

            dataGridView1.Refresh();
        }

        private SQLiteConnection SetConnection()
        {
            string DataSource = "Data Source=";
            DataSource = string.Concat(DataSource, FormIniz.cartellaSimb, ";");
            SQLiteConnection conn = new SQLiteConnection(DataSource);
            return (conn);
        }

        private string aggiungiApostrofo(string orig)
        {
            string s1 = "";
            string s2 = ""; string s3 = "";
            int ipos = 0, len = 0;
            int ipos1 = 0;
            s2 = orig;

        L10: ipos = s2.IndexOf('\x27', ipos1);

            if (ipos > 0)
            {
                len = s2.Length;
                s1 = s2.Substring(0, ipos);
                s1 = string.Concat(s1, "\x27\x27");
                s3 = s2.Substring(ipos + 1, len - ipos - 1);
                s1 = string.Concat(s1, s3);
                s2 = s1;
                ipos1 = ipos + 2;
                goto L10;
            }

            return (s2);
        }




    }
}